import java.util.ArrayList;
import java.util.List;

public class Solution199 {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        res.add(root.val);
        List<Integer> left = rightSideView(root.left);
        List<Integer> right = rightSideView(root.right);
        res.addAll(right);
        if (right.size() < left.size()) {
            res.addAll(left.subList(right.size(), left.size()));
        }
        return res;
    }
}
